<template>

  <UploadExcel :on-success="onSuccess" />
</template>

<script>
import { importEmployeeAPI } from '@/api/company'
export default {
  methods: {
    async onSuccess(data) {
      console.log(data)
      console.log(data.header)
      console.log(data.results)
      const newData = data.results.map(user => {
        return this.zhToEn(user)
      })
      console.log(newData)
      await importEmployeeAPI(newData)
      this.$message.success('导入成功')
      this.$router.back()
    },
    zhToEn(user) {
      console.log('旧', user)
      const newUser = {}
      // 这里是翻译逻辑
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      for (const key in user) {
        console.log('key', key)
        const enkey = dict[key]
        console.log('dictkey', dict[key])
        const value = user[key]
        console.log('userkey', user[key])
        // newUser[enkey] = value
        // 转换日期
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          newUser[enkey] = new Date(this.formatExcelTime(value))
        } else {
          newUser[enkey] = value
        }
      }
      console.log('新', newUser)
      return newUser
    },
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style>

</style>
